<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>养娃知识库管理</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f4f4f9;
            color: #333;
            margin: 0;
        }

        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 0px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            background-color: white;
        }

        th, td {
            border: 1px solid #e0e0e0;
            padding: 12px 15px;
            text-align: left;
        }

        /* 新增：限制类别描述列的最大宽度 */
        th:nth-child(3), td:nth-child(3) {
            max-width: 200px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        th {
            background-color: #2c3e50;
            color: white;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #e0f7fa;
        }

        .pagination {
            margin-top: 30px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .pagination a {
            color: #2c3e50;
            padding: 8px 16px;
            text-decoration: none;
            transition: background-color .3s;
            border: 1px solid #ddd;
            margin: 0 4px;
        }

        .pagination a.active {
            background-color: #2c3e50;
            color: white;
            border: 1px solid #2c3e50;
        }

        .pagination a:hover:not(.active) {
            background-color: #ddd;
        }

        .pagination a.disabled {
            color: #aaa;
            pointer-events: none;
        }

        .pagination span {
            margin: 0 10px;
            font-size: 16px;
            color: #2c3e50;
        }

        /* 新增添加按钮样式 */
        .add-button {
            padding: 10px 15px;
            background-color: #2c3e50;
            color: white;
            text-align: center;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color .3s;
        }

        .add-button:hover {
            background-color: #34495e;
        }

        /* 新增按钮容器样式 */
        .button-container {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 20px;
        }
    </style>
    <script>
        // 从父页面获取 loadPage 函数
        function loadPage(url) {
            if (window.parent && window.parent.loadPage) {
                window.parent.loadPage(url);
            }
        }
    </script>
</head>
<body>
<h1>养娃知识库管理</h1>
<div class="button-container">
    <a href="{{ url_for('add_new_knowledge') }}" onclick="loadPage(this.href); return false;"
       class="add-button">添加新知识</a>
</div>
<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>类别</th>
        <th>类别描述</th>
        <th>适合月龄</th>
        <th>适合性别</th>
        <th>编辑</th>
        <th>删除</th>
    </tr>
    </thead>
    <tbody>
    {% for item in curPageItems %}
    <tr>
        <td>{{ item['id'] }}</td>
        <td>{{ item['name'] }}</td>
        <td>{{ item['description'] }}</td>
        <td>{{ item['suitable_age'] }}</td>
        <td>{{ item['suitable_gender'] }}</td>
        <td>
            <a href="#" onclick="loadPage('{{ url_for('edit_knowledge', knowledge_id=item['id']) }}'); return false;">编辑</a>
        </td>
        <td>
            <a href="#"
               onclick="loadPage('{{ url_for('delete_knowledge', knowledge_id=item['id'])}}'); return false;">删除</a>
        </td>
    </tr>
    {% endfor %}
    </tbody>
</table>
<div class="pagination">
    {% if pagination['curPage'] > 1 %}
    <a href="#"
       onclick="loadPage('{{ url_for('view_knowledge_items', curPage=0, search=search) }}'); return false;">首页</a>
    <a href="#"
       onclick="loadPage('{{ url_for('view_knowledge_items', curPage=pagination['curPage'] - 1, search=search) }}'); return false;">上一页</a>
    {% else %}
    <a href="#" class="disabled" aria-disabled=true>首页</a>
    <a href="#" class="disabled" aria-disabled=true>上一页</a>
    {% endif %}

    {% set start_page = pagination['curPage'] - 1 if pagination['curPage'] > 1 else 1 %}
    {% set end_page = start_page + 2 if start_page + 2 <= pagination['totalPage'] else pagination['totalPage'] %}
    {% if start_page > 1 %}
    <span>...</span>
    {% endif %}
    {% for page in range(start_page, end_page + 1) %}
    {% if pagination['curPage'] == page %}
    <a class="active" href="#"
       onclick="loadPage('{{ url_for('view_knowledge_items', curPage=page, search=search) }}'); return false;">{{ page
        }}</a>
    {% else %}
    <a href="#" onclick="loadPage('{{ url_for('view_knowledge_items', curPage=page, search=search) }}'); return false;">{{
        page }}</a>
    {% endif %}
    {% endfor %}
    {% if end_page < pagination['totalPage'] %}
    <span>...</span>
    {% endif %}

    <span>共{{ pagination['totalPage'] }}页</span>
    {% if pagination['curPage'] < pagination['totalPage'] %}
    <a href="#"
       onclick="loadPage('{{ url_for('view_knowledge_items', curPage=pagination['curPage'] + 1, search=search) }}'); return false;">下一页</a>
    <a href="#"
       onclick="loadPage('{{ url_for('view_knowledge_items', curPage=pagination['totalPage'], search=search) }}'); return false;">末页</a>
    {% else %}
    <a href="#" class="disabled" aria-disabled=true>下一页</a>
    <a href="#" class="disabled" aria-disabled=true>末页</a>
    {% endif %}
</div>
</body>
</html>
